//
//  KLHistoricalRateCurrencyView.m
//  KLCurrency
//
//  Created by apple on 2018/1/5.
//  Copyright © 2018年 bonc. All rights reserved.
//

#import "KLHistoricalRateCurrencyView.h"

@interface KLHistoricalRateCurrencyView ()

@property (weak, nonatomic) IBOutlet UILabel *localLabel;
@property (weak, nonatomic) IBOutlet UILabel *exchangeLabel;
@property (weak, nonatomic) IBOutlet UIImageView *localIcon;
@property (weak, nonatomic) IBOutlet UIImageView *exchangeIcon;

@end

@implementation KLHistoricalRateCurrencyView

+ (instancetype)historicalRateCurrencyView {
    return [[[NSBundle mainBundle] loadNibNamed:@"KLHistoricalRateCurrencyView" owner:nil options:nil] firstObject];
}

- (void)setLocalCurrency:(NSString *)localCurrency {
    _localCurrency = localCurrency;
    self.localLabel.text = localCurrency;
    self.localIcon.image = [UIImage imageNamed:localCurrency];
}

- (void)setExchangeCurrency:(NSString *)exchangeCurrency {
    _exchangeCurrency = exchangeCurrency;
    self.exchangeLabel.text = exchangeCurrency;
    self.exchangeIcon.image = [UIImage imageNamed:exchangeCurrency];
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder withLocalCurrency:(NSString *)localAbb withExchangeCurrency:(NSString *)exchangeAbb {
    self = [self initWithCoder:aDecoder];
    if (self) {
        self.localLabel.text = localAbb;
        self.exchangeLabel.text = exchangeAbb;
        self.localIcon.image = [UIImage imageNamed:localAbb];
        self.exchangeIcon.image = [UIImage imageNamed:exchangeAbb];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame withLocalCurrency:(NSString *)localAbb withExchangeCurrency:(NSString *)exchangeAbb {
    self = [self initWithFrame:frame];
    if (self) {
        self.localLabel.text = localAbb;
        self.exchangeLabel.text = exchangeAbb;
        self.localIcon.image = [UIImage imageNamed:localAbb];
        self.exchangeIcon.image = [UIImage imageNamed:exchangeAbb];
    }
    return self;
}

- (IBAction)exchangeAction {
    NSString *bufferAbb = _localLabel.text;
    UIImage *bufferImage = _localIcon.image;
    
    _localLabel.text = _exchangeLabel.text;
    _localIcon.image = _exchangeIcon.image;
    
    _exchangeIcon.image = bufferImage;
    _exchangeLabel.text = bufferAbb;
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(exchangeCurrencies)]) {
        [self.delegate exchangeCurrencies];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
